var AuthorityForm = function () {
  var template = /* html */ `
    <div class="authority-form" v-loading.fullscreen.lock="loading">
      <div class="base-info">
        <span>公司名称:{{company.name}}</span>
      </div>
      <div class="form-search">
        <el-input v-model="searchValue" size="mini" style="width:220px" placeholder="输入关键字进行过滤"></el-input>
        <el-button size="mini" type="primary" @click="save">保存</el-button>
      </div>
      <div class="authority-form-tree">
        <el-tree show-checkbox ref="authorityTree" :data="authorityList" node-key="id" :props="treeProps"
          :filter-node-method="filterNode"></el-tree>
      </div>
    </div>
  `;

  return {
    props: {
      company: {
        type: Object,
        required: true
      }
    },

    watch: {
      searchValue: function (val) {
        this.$refs.authorityTree.filter(val);
      }
    },

    data: function () {
      return {
        loading: false,
        authorityList: [],
        searchValue: '',
        treeProps: {
          label: 'name'
        }
      };
    },

    created: function () {
      this.setAuthorityList();
    },

    methods: {
      setAuthorityList: function () {
        var companyName = this.company.name;
        this.loading = true;
        var self = this;
        ajaxRequest('permission/getCompanyPermission', {
          company: companyName
        }).then(function (res) {
          var permissionList = [];
          var nonPermissionList = res.notHavePermission;
          if (!SysUtil.isEmpty(nonPermissionList)) {
            permissionList.pushArray(nonPermissionList);
          }

          var checkIds = [];
          var hasPermissionList = res.havePermission;
          if (!SysUtil.isEmpty(hasPermissionList)) {
            checkIds = _.map(hasPermissionList, 'id');
            permissionList.pushArray(hasPermissionList);
          }

          permissionList = SysUtil.sortDataByField(permissionList, 'name');
          var permissionObj = {};
          self.setPermissionObj(permissionObj, permissionList);

          var authorityList = [];
          for (var id in permissionObj) {
            var permission = permissionObj[id];
            var pId = permission.pId;
            if (pId === null) {
              authorityList.push(permission);
              continue;
            }

            var obj = permissionObj[pId];
            var children = obj.children;
            if (children === undefined) {
              children = [];
              obj.children = children;
            }
            children.push(permission);
          }
          self.authorityList = authorityList;
          self.loading = false;

          self.$nextTick(function () {
            if (checkIds.length > 0) {
              self.$refs['authorityTree'].setCheckedKeys(checkIds);
            }
          });
        }).catch(function () {
          self.loading = false;
        });
      },

      setPermissionObj: function (permissionObj, permissionList) {
        for (var i = 0; i < permissionList.length; i++) {
          var permission = permissionList[i];
          var name = permission.name;
          var pathList = name.split('-');
          var len = pathList.length;
          var pId = null;
          for (var j = 0; j < len - 1; j++) {
            var pathName = pathList.slice(0, j + 1).join('-');
            var obj = permissionObj[pathName];
            if (obj !== undefined) {
              pId = pathName;
              continue;
            }

            var path = pathList[j];
            permissionObj[pathName] = {
              id: pathName,
              name: path,
              pId: pId
            };
            pId = pathName;
          }

          var id = permission.id;
          permissionObj[id] = {
            id: id,
            name: pathList[len - 1],
            pId: pId
          };
        }
      },

      save: function () {
        var tree = this.$refs['authorityTree'];
        var ids = tree.getCheckedKeys(true);

        this.loading = true;
        var self = this;
        ajaxRequest('permission/setCompanyPermission', {
          company: this.company.name,
          permissionIdArray: JSON.stringify(ids)
        }).then(function () {
          self.$alert('公司的权限设置成功', '信息', {
            callback: function () {
              self.$emit('save-success');
            }
          });
          self.loading = false;
        }).catch(function () {
          self.loading = false;
        });
      },

      filterNode: function (value, data) {
        if (!value) return true;
        return data.name.indexOf(value) !== -1;
      }
    },

    template: template
  };
}();
